package cc.booku.owlbox.UserSettingsPage;

import android.content.Intent;
import android.graphics.Color;
import android.os.Environment;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.HomeDirectory.FileEditActivity;
import cc.booku.owlbox.R;

public class SettingActivity extends AppCompatActivity {

    private Toolbar Toolbar;
    private Switch sw_filecheck, sw_softupdate, sw_driving, sw_demo, sw_server;
    private RadioGroup rg_webijk;
    private RadioButton rb_web, rb_ijk;
    private TextView tv_drving;
    private String ProfilePath = Environment.getExternalStorageDirectory() + File.separator + "OwlDatabase" + File.separator + "Profile.txt";
    private String filePath = Environment.getExternalStorageDirectory() + File.separator + "OwlDatabase" + File.separator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        //加载标题栏
        LoadToolbar();
        //状态栏颜色
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));

        rg_webijk = (RadioGroup) findViewById(R.id.rg_webijk);

        rb_ijk = (RadioButton) findViewById(R.id.rb_ijk);
//        if(rb_ijk.isChecked()){
//            SettingProfile("默认播放器","ijk");
//        }
        rb_web = (RadioButton) findViewById(R.id.rb_web);
//        if(rb_web.isChecked()){
//            SettingProfile("默认播放器","web");
//
//        }
        rg_webijk.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {

                    case R.id.rb_web:
                        SettingProfile("默认播放器", "web");
                        break;

                    case R.id.rb_ijk:
                        SettingProfile("默认播放器", "ijk");
                        break;
                }

            }
        });


        //初始化
        init_RadioButton();

        sw_server = (Switch) findViewById(R.id.sw_server);
        sw_server.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Todo

                    //SettingProfile("检测服务器状态", "yes");

                } else {
                    //Todo
                    //SettingProfile("检测服务器状态", "no");
                }
            }
        });
        tv_drving = (TextView) findViewById(R.id.tv_drving);
        sw_demo = (Switch) findViewById(R.id.sw_demo);
        sw_demo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Todo

                    //SettingProfile("是否需要案例", "yes");

                } else {
                    //Todo
                    //SettingProfile("是否需要案例", "no");
                }
            }
        });

        sw_driving = (Switch) findViewById(R.id.sw_driving);

        sw_driving.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Todo

                    SettingProfile("加密敏感资源", "yes");
                    tv_drving.setText("加密开车资源");

                } else {
                    //Todo
                    SettingProfile("加密敏感资源", "no");
                    tv_drving.setText("秋名山解除限制");
                }
            }
        });
        sw_softupdate = (Switch) findViewById(R.id.sw_softupdate);
        sw_softupdate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Todo

                    SettingProfile("软件版本检查", "yes");

                } else {
                    //Todo
                    SettingProfile("软件版本检查", "no");
                }
            }
        });

        sw_filecheck = (Switch) findViewById(R.id.sw_filecheck);
        sw_filecheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Todo

                    //SettingProfile("配置文件校验", "yes");

                } else {
                    //Todo
                    //SettingProfile("配置文件校验", "no");
                }
            }
        });
        init_Switch();
    }

    private void init_RadioButton() {

        String ProfileStr = FileoOperations.readFromSDFile(this, ProfilePath);
        //读取配置文件信息
        try {
            JSONObject query = JSON.parseObject(ProfileStr);
            String result = query.getString("默认播放器");
            if (result.equals("ijk")) {
                //rg_webijk.check(rb_ijk.getId());
                rb_ijk.setChecked(true);
            }
            if (result.equals("web")) {
                //rg_webijk.check(rb_web.getId());
                rb_web.setChecked(true);
            }

        } catch (Exception e) {

            Toast.makeText(SettingActivity.this, "配置发生错误", Toast.LENGTH_SHORT).show();
//异常处理
        }

    }

    private void LoadToolbar() {
        //新页面接收数据


        Toolbar = (Toolbar) findViewById(R.id.toolbar);

        Toolbar.setTitle("Profile配置");

        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });
        //添加menu 菜单点击事件
        Toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_edit:

                        //点击显示弹窗用于修改
                        Intent intent = new Intent(SettingActivity.this, FileEditActivity.class);
                        //用Bundle携带数据
                        Bundle bundle = new Bundle();
                        //传递name参数为specifiedFile
                        bundle.putString("FileUrlName", ProfilePath);
                        intent.putExtras(bundle);

                        startActivity(intent);

                        break;

                    case R.id.action_reload:

                        //重置
                        FileoOperations.deleteFile(SettingActivity.this, ProfilePath);
                        FileoOperations.writeData(filePath, "Profile", FileoOperations.readAssetsTxt(SettingActivity.this, "TxtData/Profile"));
                        Toast.makeText(SettingActivity.this, "配置文件重置完成", Toast.LENGTH_SHORT).show();
                        break;

                }
                return true;
            }
        });
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        getMenuInflater().inflate(R.menu.menu_profile, menu);//toolbar添加menu菜单
        return true;
    }


    private void SettingProfile(String key, String value) {


        String ProfileStr = FileoOperations.readFromSDFile(this, ProfilePath);
        //读取配置文件信息
        try {


            JSONObject query = JSON.parseObject(ProfileStr);
            query.put(key, value);
            Log.d("修改后完整数据", query.toJSONString());

            //保存到数据文件
            FileoOperations.writeAssignTxt(ProfilePath, query.toJSONString());


        } catch (Exception e) {

            Toast.makeText(SettingActivity.this, "配置发生错误", Toast.LENGTH_SHORT).show();
//异常处理
        }

    }


    private void init_Switch() {

        String ProfileStr = FileoOperations.readFromSDFile(this, ProfilePath);
        //读取配置文件信息
        try {

            JSONObject query = JSON.parseObject(ProfileStr);
//            String result = query.getString("配置文件校验");
//            if (result.equals("yes")) {
//                sw_filecheck.setChecked(true);
//            } else if (result.equals("no")) {
//                sw_filecheck.setChecked(false);
//            }

            String result2 = query.getString("软件版本检查");
            if (result2.equals("yes")) {
                sw_softupdate.setChecked(true);
            } else if (result2.equals("no")) {
                sw_softupdate.setChecked(false);
            }

            String result3 = query.getString("加密敏感资源");
            if (result3.equals("yes")) {
                sw_driving.setChecked(true);
            } else if (result3.equals("no")) {
                sw_driving.setChecked(false);
                tv_drving.setText("秋名山飙车中...");

            }

//            String result4 = query.getString("是否需要案例");
//            if (result4.equals("yes")) {
//                sw_demo.setChecked(true);
//            } else if (result4.equals("no")) {
//                sw_demo.setChecked(false);
//
//            }

//            String result5 = query.getString("检测服务器状态");
//            if (result5.equals("yes")) {
//                sw_server.setChecked(true);
//            } else if (result4.equals("no")) {
//                sw_server.setChecked(false);
//
//            }


        } catch (Exception e) {

            Toast.makeText(SettingActivity.this, "配置发生错误", Toast.LENGTH_SHORT).show();
//异常处理
        }


    }

}
